﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;
using System.Data;

namespace GrewalCRM.DAL
{
  public class CallLog_DAL : BaseDALGrewalCRM, IBaseDALGrewalCRM
  {
      #region Public Methods
      public override int AddNew(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      CallLog call = Entity as CallLog;
      Cmd.CommandText = "usp_InsertCalls";
      SetParametersForDBSave(call);    
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Update(BaseEntitiesGrewalCRM Entity)
    {
      int result = 0;
      CallLog call = Entity as CallLog;
      Cmd.CommandText = "usp_UpdateCalls";      
      SetParametersForDBSave(call);
      Cmd.Parameters.AddWithValue("@CallID", call.ID);
      Cmd.Connection.Open();
      Cmd.ExecuteNonQuery();
      result = (int)(Cmd.Parameters["@result"].Value);
      Cmd.Connection.Close();
      return result;
    }

    public override int Delete(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }
    
    public int Save(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }

    public override List<BaseEntitiesGrewalCRM> GetAll()
    {
      throw new NotImplementedException();
    }

    public List<CallLog> GetAllCallsByAdvanceSearch(SearchCriteria criteria)
    {   
      Cmd.CommandText = "usp_GetAllCallsByAdvanceSearch";

      Cmd.Parameters.Clear();
      
      if (criteria.CallNo.HasValue)
        Cmd.Parameters.AddWithValue("@CallID", criteria.CallNo.Value);
      
      if (criteria.FromDate.HasValue)
          Cmd.Parameters.AddWithValue("@FromDate", criteria.FromDate.Value);

      if (criteria.ToDate.HasValue)
          Cmd.Parameters.AddWithValue("@ToDate", criteria.ToDate.Value);     

      if (criteria.CallTypeID.HasValue)
        Cmd.Parameters.AddWithValue("@CallTypeID", criteria.CallTypeID.Value);

      if (!string.IsNullOrEmpty(criteria.DealerCounterName))
        Cmd.Parameters.AddWithValue("@DealersCounterName", criteria.DealerCounterName);

      if (!string.IsNullOrEmpty(criteria.CounterName))
        Cmd.Parameters.AddWithValue("@CounterName", criteria.CounterName);

      if (criteria.CallStatusID.HasValue)
        Cmd.Parameters.AddWithValue("@CallStatusID", criteria.CallStatusID.Value);

      if (criteria.InstallerID.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerID", criteria.InstallerID.Value);

      if (!string.IsNullOrEmpty(criteria.CustomerName.Trim()))
        Cmd.Parameters.AddWithValue("@CustomerName", criteria.CustomerName.Trim());

      if (!string.IsNullOrEmpty(criteria.Address.Trim()))
        Cmd.Parameters.AddWithValue("@Address", criteria.Address.Trim());

      if (!string.IsNullOrEmpty(criteria.ZipCode.Trim()))
        Cmd.Parameters.AddWithValue("@ZipCode", criteria.ZipCode.Trim());

      if (!string.IsNullOrEmpty(criteria.ContactNo.Trim()))
        Cmd.Parameters.AddWithValue("@ContactNo", criteria.ContactNo.Trim());

      if (criteria.ACBrandID.HasValue)
        Cmd.Parameters.AddWithValue("@ACBrandID", criteria.ACBrandID.Value);

      if (!string.IsNullOrEmpty(criteria.ACModelNo.Trim()))
        Cmd.Parameters.AddWithValue("@ACModelNo", criteria.ACModelNo.Trim());

      if (!string.IsNullOrEmpty(criteria.ACSerialNo.Trim()))
        Cmd.Parameters.AddWithValue("@ACSerialNo", criteria.ACSerialNo.Trim());

      Cmd.Parameters.AddWithValue("@IsStrictSearch", criteria.IsStrictSearch);

      Cmd.Parameters.AddWithValue("@IsTempClosed", criteria.IsTempClosed);

      Cmd.Connection.Open();
      Rdr = Cmd.ExecuteReader();
      List<CallLog> allCall = new List<CallLog>();

      if (Rdr != null && Rdr.HasRows)
      {

        while (Rdr.Read())
        {
          DateTime? CallReceiveDate;
          if (Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull)) CallReceiveDate = Rdr.GetDateTime(1); else CallReceiveDate = null;

          DealersMaster dealer = new DealersMaster(Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetInt32(2) : 0,
                                                    Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                    null, null, null, 0, true,null, null
                                                  );

          CustomersMaster customer = new CustomersMaster(Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetInt32(4) : 0,
                                                          Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetString(5) : string.Empty,
                                                          Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetString(6) : string.Empty,
                                                          Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetString(7) : string.Empty,
                                                          Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetString(8) : string.Empty,
                                                          Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetString(9) : string.Empty,
                                                          Rdr[10] != null && Rdr[10].GetType() != typeof(DBNull) ? Rdr.GetString(10) : string.Empty,
                                                          null, null
                                                        );

          ReffStatus callStatus = new ReffStatus(Rdr[11] != null && Rdr[11].GetType() != typeof(DBNull) ? Rdr.GetInt32(11) : 0,
                                                  Rdr[12] != null && Rdr[12].GetType() != typeof(DBNull) ? Rdr.GetString(12) : string.Empty,
                                                  null, null, null
                                                );

          DateTime? CallScheduledDate;
          if (Rdr[13] != null && Rdr[13].GetType() != typeof(DBNull)) CallScheduledDate = Rdr.GetDateTime(13); else CallScheduledDate = null;


          UserCRM lasrModifiedUser = new UserCRM(Rdr[22] != null && Rdr[22].GetType() != typeof(DBNull) ? Rdr.GetInt32(22) : 0);
          lasrModifiedUser.UserName = Rdr[23] != null && Rdr[23].GetType() != typeof(DBNull) ? Rdr.GetString(23) : string.Empty;

          DateTime? lastModifiedDateTime;
          if (Rdr[24] != null && Rdr[24].GetType() != typeof(DBNull)) lastModifiedDateTime = Rdr.GetDateTime(24); else lastModifiedDateTime = null;

          DateTime? callCompletedDate;
          if (Rdr[16] != null && Rdr[16].GetType() != typeof(DBNull)) callCompletedDate = Rdr.GetDateTime(16); else callCompletedDate = null;

          DateTime? acPurchasedDate;
          if (Rdr[20] != null && Rdr[20].GetType() != typeof(DBNull)) acPurchasedDate = Rdr.GetDateTime(20); else acPurchasedDate = null;

          InstallersMaster installer = new InstallersMaster(Rdr[14] != null && Rdr[14].GetType() != typeof(DBNull) ? Rdr.GetInt32(14) : 0,
                                                              Rdr[15] != null && Rdr[15].GetType() != typeof(DBNull) ? Rdr.GetString(15) : string.Empty,
                                                              null, null, null,0,true, null, null
                                                            );
          ReffCallType callType = new ReffCallType(Rdr[25] != null && Rdr[25].GetType() != typeof(DBNull) ? Rdr.GetInt32(25) : 0,
                                                  Rdr[36] != null && Rdr[36].GetType() != typeof(DBNull) ? Rdr.GetString(36) : string.Empty, null, null, null);

          ACBrands acBrand = new ACBrands(Rdr[17] != null && Rdr[17].GetType() != typeof(DBNull) ? Rdr.GetInt32(17) : 0,
                                                  Rdr[37] != null && Rdr[37].GetType() != typeof(DBNull) ? Rdr.GetString(37) : string.Empty, null, null, null);

          CallLog call = new CallLog(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                       CallReceiveDate, callType,
                                       dealer, customer, CallScheduledDate, installer, callCompletedDate,
                                       Rdr[21] != null && Rdr[21].GetType() != typeof(DBNull) ? Rdr.GetString(21) : string.Empty,
                                       Rdr[38] != null && Rdr[38].GetType() != typeof(DBNull) ? Rdr.GetString(38) : string.Empty,
                                       callStatus, acBrand, acPurchasedDate,
                                       Rdr[18].GetType() != typeof(DBNull) ? Rdr.GetString(18) : string.Empty,
                                       Rdr[19].GetType() != typeof(DBNull) ? Rdr.GetString(19) : string.Empty,
                                       Rdr[26].GetType() != typeof(DBNull) ? Rdr.GetBoolean(26) : false,
                                       Rdr[27] != null && Rdr[27].GetType() != typeof(DBNull) ? Rdr.GetDecimal(27) : 0,
                                       Rdr[28] != null && Rdr[28].GetType() != typeof(DBNull) ? Rdr.GetDecimal(28) : 0,
                                       Rdr[29] != null && Rdr[29].GetType() != typeof(DBNull) ? Rdr.GetDecimal(29) : 0,
                                       Rdr[30] != null && Rdr[30].GetType() != typeof(DBNull) ? Rdr.GetDecimal(30) : 0,
                                       Rdr[31] != null && Rdr[31].GetType() != typeof(DBNull) ? Rdr.GetDecimal(31) : 0,
                                       Rdr[32] != null && Rdr[32].GetType() != typeof(DBNull) ? Rdr.GetDecimal(32) : 0,
                                       Rdr[33] != null && Rdr[30].GetType() != typeof(DBNull) ? Rdr.GetDecimal(30) : 0,
                                       Rdr[34] != null && Rdr[31].GetType() != typeof(DBNull) ? Rdr.GetDecimal(31) : 0,
                                       Rdr[35] != null && Rdr[32].GetType() != typeof(DBNull) ? Rdr.GetDecimal(32) : 0,
                                       lasrModifiedUser,
                                       lastModifiedDateTime
                                      );
          allCall.Add(call);
        }
      }
      Cmd.Connection.Close();

      return allCall;
    }

    public List<ReportInstallationSummary> GetInstallationSummaryCashTransationReportMonthly(DateTime? FromDate, DateTime? ToDate, int? CallTypeID, int? DealerID, int? CallStatusID, int? InstallerID)
    {
      Cmd.CommandText = "usp_GetMonthlyCashTransationReport";

      Cmd.Parameters.Clear();

      if (FromDate.HasValue)
          Cmd.Parameters.AddWithValue("@FromDate", FromDate.Value);

      if (ToDate.HasValue)
          Cmd.Parameters.AddWithValue("@ToDate", ToDate.Value);

      if (CallTypeID.HasValue)
        Cmd.Parameters.AddWithValue("@CallTypeID", CallTypeID.Value);

      if (DealerID.HasValue)
        Cmd.Parameters.AddWithValue("@DealerID", DealerID.Value);

      if (CallStatusID.HasValue)
        Cmd.Parameters.AddWithValue("@CallStatusID", CallStatusID.Value);

      if (InstallerID.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerID", InstallerID.Value);   

      Cmd.Connection.Open();
      Rdr = Cmd.ExecuteReader();
      List<ReportInstallationSummary> MonyhlyReportCard = new List<ReportInstallationSummary>();

      if (Rdr != null && Rdr.HasRows)
      {

        while (Rdr.Read())
        {
         

          ReffStatus callStatus = new ReffStatus(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                  Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetString(1) : string.Empty,
                                                  null, null, null
                                                );

          ReportInstallationSummary rpt = new ReportInstallationSummary(callStatus,
                                       Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetInt32(2) : 0,
                                       Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetDecimal(3) : 0,
                                       Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetDecimal(4) : 0,
                                       Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetDecimal(5) : 0,
                                       Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetDecimal(6) : 0,
                                       Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetDecimal(7) : 0,
                                       Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetDecimal(8) : 0,
                                       Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetDecimal(9) : 0,
                                       Rdr[10] != null && Rdr[10].GetType() != typeof(DBNull) ? Rdr.GetDecimal(10) : 0,
                                       Rdr[11] != null && Rdr[11].GetType() != typeof(DBNull) ? Rdr.GetDecimal(11) : 0
                                      );
          MonyhlyReportCard.Add(rpt);
        }
      }
      Cmd.Connection.Close();

      return MonyhlyReportCard;
    }

    public List<CallLog> GetAllByCndition(int? Month, int? CallStatusID, string ZipCode)
    {

      Cmd.CommandText = "usp_GetAllCallsByCondition";

      Cmd.Parameters.Clear();

      if(Month.HasValue)
        Cmd.Parameters.AddWithValue("@Month",Month.Value);

      if(CallStatusID.HasValue)
        Cmd.Parameters.AddWithValue("@CallStatusID", CallStatusID.Value);

      if(! string.IsNullOrEmpty( ZipCode))
        Cmd.Parameters.AddWithValue("@ZipCode", ZipCode.Trim());


      Cmd.Connection.Open();
      Rdr = Cmd.ExecuteReader();
      List<CallLog> allCall = new List<CallLog>();

      if (Rdr != null && Rdr.HasRows)
      {   
     
        while (Rdr.Read())
        {   
          DateTime? CallReceiveDate;
          if(Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull)) CallReceiveDate = Rdr.GetDateTime(1); else CallReceiveDate = null;

          DealersMaster dealer = new DealersMaster(Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetInt32(2) : 0,
                                                    Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                    null,null,null,null, true, null,null
                                                  );

          CustomersMaster customer = new CustomersMaster(Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetInt32(4) : 0,
                                                          Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetString(5) : string.Empty,
                                                          Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetString(6) : string.Empty,
                                                          Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetString(7) : string.Empty,
                                                          Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetString(8) : string.Empty,
                                                          Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetString(9) : string.Empty,
                                                          Rdr[10] != null && Rdr[10].GetType() != typeof(DBNull) ? Rdr.GetString(10) : string.Empty,
                                                          null,null
                                                        );

          ReffStatus callStatus = new ReffStatus(Rdr[11] != null && Rdr[11].GetType() != typeof(DBNull) ? Rdr.GetInt32(11) : 0,
                                                  Rdr[12] != null && Rdr[12].GetType() != typeof(DBNull) ? Rdr.GetString(12) : string.Empty,
                                                  null,null,null
                                                );
         
          DateTime? CallScheduledDate;
          if(Rdr[13] != null && Rdr[13].GetType() != typeof(DBNull)) CallScheduledDate = Rdr.GetDateTime(13); else CallScheduledDate = null;

          InstallersMaster installer = new InstallersMaster(Rdr[14] != null && Rdr[14].GetType() != typeof(DBNull) ? Rdr.GetInt32(14) : 0,
                                                              Rdr[15] != null && Rdr[15].GetType() != typeof(DBNull) ? Rdr.GetString(15) : string.Empty,
                                                              null,null,null,null,true,null,null
                                                            );

          CallLog call = new CallLog(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                      CallReceiveDate,null,
                                      dealer,customer,CallScheduledDate,installer,null,
                                       Rdr[17] != null && Rdr[17].GetType() != typeof(DBNull) ? Rdr.GetString(17) : string.Empty,null,
                                       callStatus,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
                                      );
          allCall.Add(call);          
        }
      }
      Cmd.Connection.Close();

      return allCall;
    }

    public override BaseEntitiesGrewalCRM GetByID(int ID)
    {
        Cmd.CommandText = "usp_GetCallByID";
        Cmd.Parameters.Clear();
        Cmd.Parameters.AddWithValue("@CallID", ID);
        Cmd.Connection.Open();
        Rdr = Cmd.ExecuteReader();
        List<CallLog> allCall = new List<CallLog>();

        if (Rdr != null && Rdr.HasRows)
        {

            while (Rdr.Read())
            {
                DateTime? CallReceiveDate;
                if (Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull)) CallReceiveDate = Rdr.GetDateTime(1); else CallReceiveDate = null;

                DealersMaster dealer = new DealersMaster(Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetInt32(2) : 0,
                                                          Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                          null, null, null, 0, true, null, null
                                                        );

                CustomersMaster customer = new CustomersMaster(Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetInt32(4) : 0,
                                                                Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetString(5) : string.Empty,
                                                                Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetString(6) : string.Empty,
                                                                Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetString(7) : string.Empty,
                                                                Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetString(8) : string.Empty,
                                                                Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetString(9) : string.Empty,
                                                                Rdr[10] != null && Rdr[10].GetType() != typeof(DBNull) ? Rdr.GetString(10) : string.Empty,
                                                                null, null
                                                              );

                ReffStatus callStatus = new ReffStatus(Rdr[11] != null && Rdr[11].GetType() != typeof(DBNull) ? Rdr.GetInt32(11) : 0,
                                                        Rdr[12] != null && Rdr[12].GetType() != typeof(DBNull) ? Rdr.GetString(12) : string.Empty,
                                                        null, null, null
                                                      );

                DateTime? CallScheduledDate;
                if (Rdr[13] != null && Rdr[13].GetType() != typeof(DBNull)) CallScheduledDate = Rdr.GetDateTime(13); else CallScheduledDate = null;


                UserCRM lasrModifiedUser = new UserCRM(Rdr[22] != null && Rdr[22].GetType() != typeof(DBNull) ? Rdr.GetInt32(22) : 0);
                lasrModifiedUser.UserName = Rdr[23] != null && Rdr[23].GetType() != typeof(DBNull) ? Rdr.GetString(23) : string.Empty;

                DateTime? lastModifiedDateTime;
                if (Rdr[24] != null && Rdr[24].GetType() != typeof(DBNull)) lastModifiedDateTime = Rdr.GetDateTime(24); else lastModifiedDateTime = null;

                DateTime? callCompletedDate;
                if (Rdr[16] != null && Rdr[16].GetType() != typeof(DBNull)) callCompletedDate = Rdr.GetDateTime(16); else callCompletedDate = null;

                DateTime? acPurchasedDate;
                if (Rdr[20] != null && Rdr[20].GetType() != typeof(DBNull)) acPurchasedDate = Rdr.GetDateTime(20); else acPurchasedDate = null;

                InstallersMaster installer = new InstallersMaster(Rdr[14] != null && Rdr[14].GetType() != typeof(DBNull) ? Rdr.GetInt32(14) : 0,
                                                                    Rdr[15] != null && Rdr[15].GetType() != typeof(DBNull) ? Rdr.GetString(15) : string.Empty,
                                                                    null, null, null, 0, true, null, null
                                                                  );
                ReffCallType callType = new ReffCallType(Rdr[25] != null && Rdr[25].GetType() != typeof(DBNull) ? Rdr.GetInt32(25) : 0,
                                                        Rdr[36] != null && Rdr[36].GetType() != typeof(DBNull) ? Rdr.GetString(36) : string.Empty, null, null, null);

                ACBrands acBrand = new ACBrands(Rdr[17] != null && Rdr[17].GetType() != typeof(DBNull) ? Rdr.GetInt32(17) : 0,
                                                        Rdr[37] != null && Rdr[37].GetType() != typeof(DBNull) ? Rdr.GetString(37) : string.Empty, null, null, null);

                CallLog call = new CallLog(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                             CallReceiveDate, callType,
                                             dealer, customer, CallScheduledDate, installer, callCompletedDate,
                                             Rdr[21] != null && Rdr[21].GetType() != typeof(DBNull) ? Rdr.GetString(21) : string.Empty,
                                             Rdr[38] != null && Rdr[38].GetType() != typeof(DBNull) ? Rdr.GetString(38) : string.Empty,
                                             callStatus, acBrand, acPurchasedDate,
                                             Rdr[18].GetType() != typeof(DBNull) ? Rdr.GetString(18) : string.Empty,
                                             Rdr[19].GetType() != typeof(DBNull) ? Rdr.GetString(19) : string.Empty,
                                             Rdr[26].GetType() != typeof(DBNull) ? Rdr.GetBoolean(26) : false,
                                             Rdr[27] != null && Rdr[27].GetType() != typeof(DBNull) ? Rdr.GetDecimal(27) : 0,
                                             Rdr[28] != null && Rdr[28].GetType() != typeof(DBNull) ? Rdr.GetDecimal(28) : 0,
                                             Rdr[29] != null && Rdr[29].GetType() != typeof(DBNull) ? Rdr.GetDecimal(29) : 0,
                                             Rdr[30] != null && Rdr[30].GetType() != typeof(DBNull) ? Rdr.GetDecimal(30) : 0,
                                             Rdr[31] != null && Rdr[31].GetType() != typeof(DBNull) ? Rdr.GetDecimal(31) : 0,
                                             Rdr[32] != null && Rdr[32].GetType() != typeof(DBNull) ? Rdr.GetDecimal(32) : 0,
                                             Rdr[33] != null && Rdr[30].GetType() != typeof(DBNull) ? Rdr.GetDecimal(30) : 0,
                                             Rdr[34] != null && Rdr[31].GetType() != typeof(DBNull) ? Rdr.GetDecimal(31) : 0,
                                             Rdr[35] != null && Rdr[32].GetType() != typeof(DBNull) ? Rdr.GetDecimal(32) : 0,
                                             lasrModifiedUser,
                                             lastModifiedDateTime
                                            );
                allCall.Add(call);
            }
        }
        Cmd.Connection.Close();

        return allCall[0];
    }

    public override void FillEntity(BaseEntitiesGrewalCRM Entity)
    {
      throw new NotImplementedException();
    }
    #endregion

    #region Private Method
    private void SetParametersForDBSave(CallLog call)
    {
      Cmd.Parameters.Clear();
      Cmd.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

      if (call.CallReceiveDate != null)
        Cmd.Parameters.AddWithValue("@CallReceiveDate", call.CallReceiveDate);

      if (null != call.CallType && call.CallType.ID != 0)
        Cmd.Parameters.AddWithValue("@CallTypeID", call.CallType.ID);

      if (null != call.Dealer && call.Dealer.ID != 0)
        Cmd.Parameters.AddWithValue("@DealerID", call.Dealer.ID);

      if (call.CallScheduledDate != null)
        Cmd.Parameters.AddWithValue("@CallScheduledDate", call.CallScheduledDate);

      if (null != call.Installer && call.Installer.ID != 0)
        Cmd.Parameters.AddWithValue("@InstallerID", call.Installer.ID);

      if (call.CallCompletedDate != null)
        Cmd.Parameters.AddWithValue("@CallCompletedDate", call.CallCompletedDate);

      if (call.Remarks != null && call.Remarks.Trim() != string.Empty)
        Cmd.Parameters.AddWithValue("@Remarks", call.Remarks);

      if (call.ACBrand != null)
        Cmd.Parameters.AddWithValue("@ACBrandID", call.ACBrand.ID);

      if (call.ACPurchasedDate != null)
        Cmd.Parameters.AddWithValue("@ACParchasedDate", call.ACPurchasedDate);

      if (call.ACModelNo != null)
        Cmd.Parameters.AddWithValue("@ACModel", call.ACModelNo);

      if (call.ACSerialNo != null)
        Cmd.Parameters.AddWithValue("@ACSerialNo", call.ACSerialNo);

      if (null != call.CallStatus && call.CallStatus.ID != 0)
        Cmd.Parameters.AddWithValue("@CallStatusID", call.CallStatus.ID);

      if (null != call.IsTempClosed && call.IsTempClosed.HasValue)
        Cmd.Parameters.AddWithValue("@IsTempClosed", call.IsTempClosed.Value);

      if (null != call.InstallerCommisionCR && call.InstallerCommisionCR.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerCommisionCR", call.InstallerCommisionCR.Value);

      if (null != call.InstallerMaterialCR && call.InstallerMaterialCR.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerMaterialCR", call.InstallerMaterialCR.Value);

      if (null != call.InstallerAcDR && call.InstallerAcDR.HasValue)
        Cmd.Parameters.AddWithValue("@InstallerAcDR", call.InstallerAcDR.Value);

      if (null != call.DealerAcDR && call.DealerAcDR.HasValue)
        Cmd.Parameters.AddWithValue("@DealerAcDR", call.DealerAcDR.Value);

      if (null != call.DealerAcCR && call.DealerAcCR.HasValue)
        Cmd.Parameters.AddWithValue("@DealerAcCR", call.DealerAcCR.Value);

      if (null != call.Customer && call.Customer.ID != 0)
        Cmd.Parameters.AddWithValue("@CustomerID", call.Customer.ID);

      if (null != call.Customer && call.Customer.CustomerName != null)
        Cmd.Parameters.AddWithValue("@CustomerName", call.Customer.CustomerName);

      if (null != call.Customer && call.Customer.Address != null)
        Cmd.Parameters.AddWithValue("@Address", call.Customer.Address);

      if (null != call.Customer && call.Customer.ZipCode != null)
        Cmd.Parameters.AddWithValue("@ZipCode", call.Customer.ZipCode);

      if (null != call.Customer && call.Customer.PhoneNo != null)
        Cmd.Parameters.AddWithValue("@PhoneNo", call.Customer.PhoneNo);

      if (null != call.Customer && call.Customer.MobileNo1 != null)
        Cmd.Parameters.AddWithValue("@MobileNo1", call.Customer.MobileNo1);

      if (null != call.Customer && call.Customer.MobileNo2 != null)
        Cmd.Parameters.AddWithValue("@MobileNo2", call.Customer.MobileNo2);

      if (null != call.LastModifiedUser && call.LastModifiedUser.ID != 0)
        Cmd.Parameters.AddWithValue("@LastModifiedUser", call.LastModifiedUser.ID);
    } 
    #endregion
  }
}
